package com.cction.activity.tax

import android.app.AlertDialog
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.cction.R
import com.cction.comm.T
import com.umeng.analytics.MobclickAgent
import kotlinx.android.synthetic.main.tax_main.*
import kotlinx.android.synthetic.main.title_bar.*

/**
 * 个税计算
 */
class TaxActivity : AppCompatActivity() {
    private val context = this

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.tax_main)

        initView()
    }


    private fun initView() {
        title_bar_back.visibility = View.VISIBLE
        title_bar_back.setOnClickListener { finish() }

        title_bar_title.text = "个税计算"
        title_bar_other.visibility = View.GONE


        tax_minimum_image.setOnClickListener {
            showDefineDialog("2018年10月开始，起征点调整为5000")
        }
        tax_insurance_image.setOnClickListener {
            showDefineDialog("部分人缴纳的是三险一金，计算方式一样")
        }
        tax_specially_image.setOnClickListener {
            showDefineDialog("专项扣除主要包括：\n" +
                    "1，子女教育支出\n" +
                    "2，继续教育支出\n" +
                    "3,大病医疗支出\n" +
                    "4,住房贷款利息\n" +
                    "5,住房租金\n" +
                    "6,赡养老人费用")
        }
        tax_calculate.setOnClickListener {
            if (checkData()) {
                val total = tax_total_edit.text.toString()
                val minimum = tax_minimum_edit.text.toString()
                val insurance = tax_insurance_edit.text.toString()
                val specially = tax_specially_edit.text.toString()

                val intent = Intent()
                intent.setClass(context, TaxDetailActivity::class.java)
                intent.putExtra("tax_total_edit", total)
                intent.putExtra("tax_minimum_edit", minimum)
                intent.putExtra("tax_insurance_edit", insurance)
                intent.putExtra("tax_specially_edit", specially)
                startActivity(intent)
            }
        }
    }


    /**
     * 数据检测
     */
    private fun checkData(): Boolean {
        val total = tax_total_edit.text.toString()
        if (TextUtils.isEmpty(total)) {
            T.showShort(this, "税前工资不能为空")
            return false
        }

        val insurance = tax_insurance_edit.text.toString()
        if (TextUtils.isEmpty(insurance)) {
            T.showShort(this, "五险一金不能为空")
            return false
        }

        val specially = tax_specially_edit.text.toString()
        if (TextUtils.isEmpty(specially)) {
            T.showShort(this, "专项扣除不能为空")
            return false
        }

        if (total.toDouble() - insurance.toDouble() - specially.toDouble() <= 0) {
            T.showShort(this, "五险一金与专项扣总和不能大于每月工资")
            return false
        }

        return true
    }


    /**
     * 显示对话框
     */
    private fun showDefineDialog(value: String) {
        val builder = AlertDialog.Builder(this);
        builder.setTitle("提示")
        builder.setMessage(value)
        builder.setPositiveButton("确定") { _, _ ->

        }
        builder.show()
    }


    public override fun onResume() {
        super.onResume()
        MobclickAgent.onResume(this)
    }

    public override fun onPause() {
        super.onPause()
        MobclickAgent.onPause(this)
    }
}
